/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
/*
 * A custom table cell renderer to simulate the look of cells in
 * Apple's brushed metal applications.
 * <p/>
 * Copyright (C) 2005 by Jon Lipsky
 * <p/>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may 
 * obtain a copy of the License at
 * <p/>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p/>
 * Unless required by applicable law or agreed to in writing, software d
 * istributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.isqlviewer.ui.laf;

import java.awt.Color;
import java.awt.Component;
import java.awt.Font;

import javax.swing.Icon;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

import org.isqlviewer.swing.SwingUtilities;
import org.isqlviewer.swing.table.EnhancedTableModel;

/**
 * Modified version of the Aqua&cop; metal look and feel.
 * <p>
 * 
 * @author Jon Lipsky, Mark Kobold
 * @version 1.0
 */
public class EnhancedTableCellRenderer extends DefaultTableCellRenderer {

    private static final long serialVersionUID = -6332850927184223196L;
    public static final Color selectedFocusedColor = Color.decode("#3D80DF");
    public static final Color selectedNotFocusedColor = Color.decode("#C0C0C0");
    public static final Color evenRowColor = Color.decode("#EDF3FE");
    public static final Color oddRowColor = Color.WHITE;
    public static final Color gridColor = new Color(150, 150, 150);

    // ------------------------------------------------------------------------------------------------------------------
    // Constructors and Getter/Setter methods
    // ------------------------------------------------------------------------------------------------------------------
    public EnhancedTableCellRenderer() {

    }

    public Icon getIcon(Object aObject) {

        if (aObject == null) {
            setHorizontalAlignment(CENTER);
        } else {
            setHorizontalAlignment(LEFT);
        }
        return aObject == null ? SwingUtilities.loadIconResource("null_data", 16) : null;
    }

    public String getText(Object aObject) {

        return aObject == null ? null : aObject.toString();
    }

    // ------------------------------------------------------------------------------------------------------------------
    // Overwridden methods from the superclass(es)
    // ------------------------------------------------------------------------------------------------------------------
    @Override
    public Component getTableCellRendererComponent(final JTable table, Object value, boolean isSelected,
            boolean hasFocus, final int row, int column) {

        super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
        if (isSelected) {
            if (table.hasFocus()) {
                setBackground(selectedFocusedColor);
                setForeground(Color.WHITE);
            } else {
                setBackground(selectedNotFocusedColor);
                setForeground(Color.BLACK);
            }
        } else {
            Color color = row % 2 == 0 ? evenRowColor : oddRowColor;
            setBackground(color);
            setForeground(Color.BLACK);
        }
        setText(getText(value));
        setIcon(getIcon(value));

        if (table.getModel() instanceof EnhancedTableModel) {
            EnhancedTableModel model = (EnhancedTableModel) table.getModel();
            Font fnt = table.getFont();
            if (model.matchesFilter(row, column)) {
                // increase the font by one point so we have size and hue for highlighting searches//
                setFont(fnt.deriveFont(Font.BOLD, fnt.getSize2D() + 1f));
            } else {
                setFont(fnt);
            }
        }
        return this;
    }
}